<?php
/**
 * WpLogBase.php
 * 
 * This file contains the WpLogBase class which provides basic functionality for all log instances.
 * This class is abstract so it must be extended and the method _log2x must be written.
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage base.wplog
 * @dependencies: wildpack.base.observe.*
 */

/**
 * The WpLog interface to implement
 */
require_once(dirname(__FILE__).'/IWpLog.php');

if (!class_exists('WpObservable')) {
	die('Fatal error: You must include the observe package in order to use the connector package. Script stopped!');
}

/**
 * The abstract base class for all log implementations
 * 
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage base.wplog
 * @dependencies: wildpack.base.observe.*
 * @event logaction(log)
 * @event logerror(log)
 * @event logmessage(log)
 * @event logaudit(log)
 */
abstract class WpLogBase extends WpObservable
{
	/**
   * The log level
   */
	protected $_intLevel = 1;
	
	/**
	 * Logs an entry
	 * @param object $logObject The log entry to save
	 */
	protected abstract function _log2x($logObject);

	
	/** 
	 * The class constructor
	 */
	public function __construct()
	{
		$this->addEvents(array('error', 'logerror', 'logaction', 'logmessage', 'logaudit'));
	}
	
	/**
   * Retrieves the current log level
   * @return The current log level
   */
	public function getLevel()
	{
		return $this->_intLevel;
	}
	
	/** 
   * Logs a runtime action
   * @param string $location The location of the action
   * @param string $user The logged-in user
   * @param string $title The action title
   * @param string $body The action body 
   * @param string $number The error number
   * @param int $level The level of this action [optional]
   * @return True if successfully processed, false else
   */
	public function logAction($location, $user, $title, $body, $number, $level=1)
	{
		if ($level > $this->getLevel()) {
			return;
		}
		
		$l = array(
			'type' => 'action',
			'location' => $location,
			'user' => $user,
			'date' => date('c'),
			'title' => $title,
			'body' => $body,
			'number' => $number,
			'level' => $level
		);
		$this->_log2x($l);
		$this->fireEvent('logaction', array($l));
	}
	
	/** 
   * Logs a runtime error
   * @param string $location The location of the error
   * @param string $user The logged-in user
   * @param string $title The error title
   * @param string $body The error body 
   * @param string $number The error number
   * @param int $level The level of this message [optional]
   * @return True if successfully processed, false else
   */
	public function logError($location, $user, $title, $body, $number, $level=1)
	{
		if ($level > $this->getLevel()) {
			return;
		}
		
		$l = array(
			'type' => 'error',
			'location' => $location,
			'user' => $user,
			'date' => date('c'),
			'title' => $title,
			'body' => $body,
			'number' => $number,
			'level' => $level
		);
		$this->_log2x($l);
		$this->fireEvent('logerror', array($l));
	}
	
  /** 
   * Logs a runtime message
   * @param string $location The location of the message
   * @param string $user The logged-in user
   * @param string $title The message title
   * @param string $body The message body 
   * @param string $number The message number
   * @param int $level The level of this message [optional]
   * @return True if successfully processed, false else
   */
	public function logMessage($location, $user, $title, $body, $number, $level=1)
	{
		if ($level > $this->getLevel()) {
			return;
		}
		
		$l = array(
			'type' => 'message',
			'location' => $location,
			'user' => $user,
			'date' => date('c'),
			'title' => $title,
			'body' => $body,
			'number' => $number,
			'level' => $level
		);
		$this->_log2x($l);
		$this->fireEvent('logmessage', array($l));
	}
	
	/** 
   * Logs an audit entry
   * @param string $location The location of the audit
   * @param string $user The logged-in user
   * @param string $title The audit title
   * @param string $body The audit body 
   * @param string $number The audit number
   */
	public function logAudit($location, $user, $title, $body, $number)
	{
		$l = array(
			'type' => 'audit',
			'location' => $location,
			'user' => $user,
			'date' => date('c'),
			'title' => $title,
			'body' => $body,
			'number' => $number
		);
		$this->_log2x($l);
		$this->fireEvent('logaudit', array($l));
	}
	
	/**
   * Sets the maximum level of the log entries to accept. The higher the level the more detailed the log will be.
   * Basically all errors and important messages should be of level 1, whereas debugging information should be of level 3 or higher.
   * @param int $level The log level to use for filtering log entries
   */
	public function setLevel($level)
	{
		$this->_intLevel = $level;
	}
}

/* End of file WpLogBase.php */
/* Location: ./wildpack/base/wplog/WpLogBase.php */ 
